// Stephanie Chaffin
  // ECE 387 LilyPad Temperature Sensor and RGB LED light
  
      /* This code reads the ambient temperature then turns on a 
     light to represent the value read. The code accepts the 
     analog reading, coverts it to voltage, then represents the 
     voltage as a temperature. 
      */
      
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  
  pinMode (11, HIGH);  //red LED
  pinMode (10, HIGH); //blue LED
  pinMode (9, HIGH); //green LED
}

// continuous loop:
void loop() {
  
  // read the temperature sensor input on analog pin 5:
  int sensorValue = analogRead(A5);
  
  // this value is expected to be 5.0V but may vary, measure this with multimeter to ensure accurate temperature reading 
  float referenceVoltage = 5.0;

  // Convert the analog reading (which goes from 0 - 1024) to a voltage (0 - 5V):
  float voltage = sensorValue * (referenceVoltage/ 1024.0);

  // Convert the voltage reading to temperatures
  float tempC = (voltage - (referenceVoltage*0.1))*100;
  float tempF = (tempC*9/5)+32;
  
  // Pin values on RGB light sensor
  int red = 11;
  int blue = 10; 
  int green = 9;
  
  // Threshold Temperatures with respect to tempF
  int cold = 70;
  int warm = 80;
  
  // To turn the light off write pin to HIGH
  digitalWrite(red, HIGH);
  digitalWrite(blue, HIGH);
  digitalWrite(green, HIGH);
  
  //print to Serial Monitor 
  Serial.print ("Voltage: ");
  Serial.println(voltage);
  Serial.print("Celsius: ");
  Serial.println(tempC);
  Serial.print("Fahrenheit: ");
  Serial.println(tempF);
  Serial.println("");
  
  // Threshold set up to RGB Sensor
    if(tempF <= cold)
      {
        digitalWrite(blue, LOW);
      }
    else if (tempF > cold && tempF <= warm)
     {
        digitalWrite(green, LOW);
     }
    else if (tempF > warm)
     {
        digitalWrite(red, LOW);
      }
      
   // sampling frequency
    delay(4000);
}
